<?php
namespace cubox\database;
//AI写的，还没测试
use cubox\Database;
use cubox\Cubox;
use cubox\provider\PDO;
use cubox\provider\SQLParam;

class Mysql extends Database{

    use PDO;
    
    public function escapeName($name):string{
        return '`'.str_replace('`', '``', $name).'`';
    }

    public function escapeValue($value){
        if(is_null($value) || $value === '' ){
            return 'NULL';
        }
        if(is_bool($value)){
            
            return $value? '1' : '0';
        }
        if(is_int($value)){
            return $value;
        }
        if(is_float($value)){
            return $value;
        }
        //real_escape_string
        if (is_string($value)) {
            return $this->connection->quote($value);
        }
    }

   

    protected function getSelectSql(string $table,SQLParam $sql):string{

        $sqlstr = parent::getSelectSql($table,$sql); 

        if ($sql->limit){
            if ($sql->limit[1]){
                $sqlstr .= ' LIMIT '.$sql->limit[0].','.$sql->limit[1];
            }else{
                $sqlstr .= ' LIMIT '.$sql->limit[0];
            }
            
        }

        return $sqlstr;
    }

    public static function configParams(): array
    {
        return [
            'host' => [
                'input' =>'input',
                'default' => '127.0.0.1',
                'description' => 'The MySQL host.',
            ],
            'port' => [
                'input' =>'input',
                'default' => 3306,
                'description' => 'The MySQL port.',
            ],
            'user' => [
                'input' =>'input',
                'default' => 'root',
                'description' => 'The MySQL user.',
            ],
            'password' => [
                'input' =>'input',
            ],
            'database' => [
                'input' =>'input',
            ],
        ];
    }

    public static function frontTitle($config){
        $config['port'] =  $config['port'] ?? 3306;
        return $config['host'].':'.$config['port'].'/'.$config['database'];
    }
}
